home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / mac_main.c < prev    next >
Encoding:
Text File  |  1997-06-04  |  6.5 KB  |  299 lines  |  [TEXT/CWIE]

  1. // mac_main.c
  2. // 28May1997 e -- derived from uio.c for mosml
  3.  
  4. #include <Dialogs.h>
  5. #include <Events.h>
  6. #include <Files.h>
  7. #include <Stdlib.h>
  8. #include <Types.h>
  9. #include <Resources.h>
  10. #include <console.h>
  11. #include "ui.h"
  12.  
  13. #if defined( THINK_C ) || defined ( __MWERKS__ )
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <strings.h>
  17. #include "os_mac_eventchk.h"
  18. #ifdef THINK_C
  19. #include <fcntl.h>
  20. #include <stat.h>
  21. #else
  22. #include <unix.h>
  23. #endif
  24. #endif
  25.  
  26. #include <Gestalt.h>
  27.  
  28. extern Boolean gMadeAppFSS;
  29. extern char gReceivedAE;
  30. extern FSSpec appFSS;
  31. extern int sub_main (int argc, char *argv []);
  32. extern unsigned char *console_title;
  33. extern char *image_name;
  34. #ifdef THINK_C
  35. extern WindowPeek cflush(FILE *fp);
  36. #endif
  37.  
  38. // get_wd_name must copy pstr to a malloc'd C string
  39. // if relative path name, construct full path name
  40. // bashing fname is permitted
  41.  
  42. char *get_wd_name( Str255 fname )
  43. {
  44.   int len = fname[0];
  45.   char *p, *res;
  46.   if( len == 0 )
  47.   { // null string
  48.     p = (char *)&fname[0];
  49.   }
  50.   else
  51.   { // see if 1st char ':' or has no ':'
  52.     fname[len+1] = 0; // make it a bi-string
  53.     if( fname[1] == ':' )
  54.     {
  55.       fname[1] = fname[0] -1;
  56.       fname = &fname[1];
  57.       goto make_full_path;
  58.     }
  59.     else if( strchr( (char *)&fname[2], ':' ) == NULL )
  60.     { // make relative
  61.       char path[512];
  62. make_full_path:
  63.       len = getfullpath( appFSS.vRefNum, appFSS.parID, fname, path, 511, 0 );
  64.       p = path;
  65.     }
  66.     else
  67.     { // it's absolute
  68.       p = (char *)&fname[1];
  69.     }
  70.   }
  71.   res = malloc( len + 1 );
  72.   if( res != 0 ) memcpy( res, p, len + 1 );
  73.   return res;
  74. }
  75.  
  76. // puts, but quotify string suitably for ecommand
  77.  
  78. static puts_quotify( char *str )
  79. {
  80.   // fputs( str, stdout );
  81.   char buf[512];
  82.   int qsp = 0;
  83.   int qsq = 0;
  84.   int qdq = 0;
  85.   int qbs = 0;
  86.   int len = 0;
  87.   char c, *s = str;
  88.   // count funny chars
  89.   while( (c = *s++) != 0 )
  90.   {
  91.     if(      c == ' '  ) qsp++;
  92.     else if( c == '"'  ) qdq++;
  93.     else if( c == '\'' ) qsq++;
  94.     else if( c == '\\' ) qbs++;
  95.     len++;
  96.   }
  97.   if( qsp + qsq + qdq + qbs == 0 || len > 256 )
  98.   { // no funny chars
  99.     s = str; // no problem
  100.   }
  101.   else
  102.   { // maybe quotify each funny char
  103.     char q, *t = buf;
  104.     if( qsp + qsq + qdq == 0 ) q = 0;    // no outer quotes if only '\\'
  105.     else if( qdq <= qsq )      q = '"';  // else outer quote with '"' or '\''
  106.     else                       q = '\''; // whichever is used less often
  107.     if( q != 0 ) *t++ = q;
  108.     s = str;
  109.     while( (c = *s++) != 0 )
  110.     {
  111.       if( c == q || c == '\\' ) *t++ = '\\';
  112.       *t++ = c;
  113.     }
  114.     if( q != 0 ) *t++ = q;
  115.     *t = 0;
  116.     s = buf;
  117.   }
  118.   fputs(   s, stdout );
  119.   fputs( " ", stdout );
  120. }
  121.  
  122. #ifdef __MC68K__
  123. #if __MC68K__
  124. static void die( unsigned char *txt )
  125. {
  126.   InitGraf( &qd.thePort );
  127.   InitFonts();
  128.   InitWindows();
  129.   InitMenus();
  130.   TEInit();
  131.   InitDialogs( 0L );
  132.   InitCursor();
  133.   ParamText( txt, "\p", "\p", "\p" );
  134.   StopAlert( ok_alertID, 0L );
  135.   ExitToShell();
  136. }
  137. #endif
  138. #endif
  139.  
  140.  
  141. static long probe_image( char *name ) // returns length or -1
  142. {
  143.   long len = -1;
  144.   int fd = open( name, O_RDONLY | O_BINARY );
  145.   if (fd != -1)
  146.   {
  147.     len = lseek( fd, 0, SEEK_END );
  148.     close( fd );
  149.   }
  150.   return len;
  151. }
  152.  
  153. #define MIN_IMAGE_SIZE 1200
  154.  
  155. main ()
  156. {
  157.   int argc, fd;
  158.   char **argv;
  159.   char *args[6];
  160.   KeyMap kMap;
  161.   long len;
  162.   Str255 pstr;
  163.  
  164.   if ( GetApplLimit() > ( LMGetCurStackBase() - 40000 ) )
  165.   { SetApplLimit ( LMGetCurStackBase() - 40000 );
  166.   }
  167.  
  168. #ifdef __MC68K__
  169. #if __MC68K__
  170. #if __MC68020__
  171.   if( /* NG on Sys 7.1 and others? -- 08Oct96 e
  172.           Gestalt( gestaltNativeCPUtype, &len ) || len < gestaltCPU68020
  173.        */
  174.         Gestalt( gestaltProcessorType, &len ) || len < gestalt68020
  175.     )
  176.   { die( "\pAn M68020 or better processor is required to run this application!" );
  177.   }
  178. #endif
  179. #if __MC68881__
  180.   if( Gestalt( gestaltFPUType, &len ) || len == gestaltNoFPU )
  181.   { die( "\pAn FPU (or M64040) is required to run this application!\
  182.  Try using the SANE version." );
  183.   }
  184. #endif
  185. #endif
  186. #endif
  187.  
  188.   // first, see if we were launched with an image document
  189.   // to do that we need to init MacOS which is done by the console
  190.   GetIndString(pstr, 357, 5); /* console title */
  191.   if ( pstr[0] == 0 )
  192.     console_title = "\pMosml Console";
  193.   else
  194.     console_title = pstr;
  195. #ifdef THINK_C
  196.   cflush(stdin);
  197. #else
  198.   puts(" ");
  199.   fflush(stdout);
  200. #endif
  201.   for( fd = 0; fd < 20 && gReceivedAE == 0; fd++ )
  202.     os_event_check(); // let any open apple events in
  203.  
  204.   if( gReceivedAE < 0 )
  205.     goto handle_AE_cmd_line; // bail if we were launched with command line
  206.  
  207.   // see if ProcessManager call set up appFSS
  208.   if( ! gMadeAppFSS )
  209.   { // if not, try to fill in the blanks
  210.     unsigned char *CurApName = LMGetCurApName();
  211.     short wd;
  212.     GetVol( NULL, &wd );
  213.     appFSS.vRefNum = 0;
  214.     GetWDInfo( wd, &appFSS.vRefNum, &appFSS.parID, &len );
  215.     if( (wd = CurApName[0]) < 64 )
  216.       memcpy( appFSS.name, CurApName, wd );
  217.   }
  218.  
  219.   /* arg 0 */
  220.   len = (image_name == NULL) ? 0 : probe_image( image_name );
  221.   if( len >= MIN_IMAGE_SIZE )
  222.   {
  223.     args[0] = image_name;
  224.   }
  225.   else
  226.   {
  227.     GetIndString(pstr, 357, 1); /* image name */
  228.     args[0] = get_wd_name( pstr );
  229.     len = probe_image( args[0] );
  230.   }
  231.   if( len >= 0 )
  232.   {
  233.     GetKeys( kMap );
  234.     // fd = ((len < 12000) || (kMap[1] & 4)) ? -1 : 0; -- option key
  235.     fd = ((len < MIN_IMAGE_SIZE) || (kMap[1] & 32768L)) ? -1 : 0;
  236.   } /* probe image, insure big enough, command key for command line */
  237.   else fd = -1;
  238.  
  239.   /* arg 1 */
  240.   args[1] = "-stdlib";
  241.  
  242.   /* arg 2 */
  243.   GetIndString(pstr, 357, 2); /* stdlib directory name */
  244.   args[2] = get_wd_name( pstr );
  245.  
  246.   argc = 3;
  247.  
  248.   /* optional arg 3 */
  249.   GetIndString(pstr, 357, 3);     /* options, e.g., -P */
  250.   if ( pstr[0] != 0 )
  251.   {
  252.     args[3] = malloc( pstr[0] );
  253.     if( args[3] != NULL )
  254.     { argc = 4;
  255.       p_to_c( pstr, args[3] );
  256.       /* optional arg 4 */
  257.       GetIndString(pstr, 357, 4); /* options, e.g., {full,none,etc.} */
  258.       if ( pstr[0] != 0 )
  259.       {
  260.         args[4] = malloc( pstr[0] );
  261.         if( args[4] != NULL )
  262.         { argc = 5;
  263.           p_to_c( pstr, args[4] );
  264.         }
  265.       }
  266.     }
  267.   }
  268.  
  269.   if ( fd != -1 )
  270.   { 
  271.     argv = args;
  272.     argv[argc] = NULL;
  273.   }
  274.   else
  275.   {
  276.     if ( 1 /* kMap[1] & 1 */  ) // shift key
  277.     {
  278.        puts_quotify( args[0] );
  279.       fputs( args[1], stdout );
  280.       fputs( " ", stdout );
  281.        puts_quotify( args[2] );
  282.       fputs( args[3], stdout );
  283.       fputs( " ", stdout );
  284.        puts( args[4] );
  285.     }
  286. handle_AE_cmd_line:
  287.     argc = ecommand( (unsigned char ***)&argv );
  288.   }
  289.  
  290.   if( argc > 0 ) // ecommand can do this now -- 13May97 e
  291.   {
  292.     init_timers();
  293.     caml_main( argc, argv );
  294.   }
  295.   return 0;
  296. }
  297.  
  298. // end of mac_main.c
  299.